package com.aube.plugin.detail;

import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TextContentTool {

    private static String replaceToBr(String content, Pattern pattern) {
        Matcher matcher = pattern.matcher(content);
        StringBuffer buffer = new StringBuffer();
        int start = 0;
        int end = 0;
        while (matcher.find()) {
            String key = matcher.group();
            start = matcher.start();

            buffer.append(content.substring(end, start)).append("<br>");
            end = start + key.length();

            if (end >= content.length())
                break;
        }
        if (end < content.length())
            buffer.append(content.substring(end));
        return buffer.toString();
    }

    /**
     * 得到一个SpanableString对象，通过传入的字符串,并进行正则判断
     * @param str
     * @return
     */
    public static SpannableString getSpanContent(String str) {
        return htmlFormWala(str);
    }


    public static SpannableString htmlFormWala(String str) {
        if (str == null) {
            str = "";
        }

        String matherBr = "\n";
        Pattern patternBr = Pattern.compile(matherBr, Pattern.CASE_INSENSITIVE);
        Spanned htmlSpan = Html.fromHtml(replaceToBr(str, patternBr));
        SpannableString spannableString = new SpannableString(htmlSpan);

        return spannableString;
    }

}